#!/usr/bin/env python
import random, string
from optparse import make_option, OptionGroup, OptionParser
global VERSION, NAME

# Password generator

NAME = 'pgen'
VERSION = '0.2'

# 0.1: simple, silly, and utterly convoluted
# 0.2: complete re-write of the password generator; not yet working

def optionize(): 
    '''Grab and set the options.'''    
    # General options to enable more functionality
    option_list = [
        make_option('-n', '--number', 
            action = 'store_true',
            help = 'include numbers'
        ),
        make_option('-s', '--special', 
            action = 'store_true',
            help = 'include special characters'
        ),
        make_option('-r', '--lower', 
            action = 'store_true',
            help = 'include lower case letters'
        ),
        make_option('-u', '--upper', 
            action = 'store_true',
            help = 'include upper case letters'
        )
    ]
    
    # Define parser
    parser = OptionParser(description = 'A simple password generator in Python.', 
        option_list = option_list,
        epilog = 'This is Python 2.4 compatible.', 
        usage = '%s -nsru -[lNSRU] [value]' % NAME, 
        version = '%s' % VERSION
    )
    
    # Group functions: 
    values_group = OptionGroup(parser, 'Values', 
        'Specify number of each type to include.'
    )
    values_group.add_option('-l', '--length', 
            help = 'password length',
            type = 'int'
    )
    values_group.add_option('-N', '--number-count', 
            help = 'number of numbers to include',
            type = 'int'
    )
    values_group.add_option('-R', '--lower-count', 
            help = 'number of lower case letters', 
            type = 'int'
    )
    values_group.add_option('-S', '--special-count', 
            type = 'int',
            help = 'number of special characters'
    )
    values_group.add_option('-U', '--upper-count', 
            help = 'number of upper case letters'
    )
    
    # Add options groups
    option_groups = [values_group]
    for each in option_groups: parser.add_option_group(each)
    
    # Define default options
    parser.set_defaults(
        lower = False, 
        number = False, 
        special = False,
        upper = False, 
        length = 0, 
        lower_count = 0, 
        number_count = 0,
        special_count = 0, 
        upper_count = 0, 
    ) 
    
    # Use options and grab arguments
    options, args = parser.parse_args()
        
    return options, args
    
def run_program(): 
    '''Run the application with the above definitions.'''
    # Get the options and arguments from above
    options, args = optionize()
    
    glob = []
    if options.number: 
        if options.number_count: 
            glob += random.sample( string.digits, options.number_count )
    
    if options.lower: 
        if options.lower_count: 
            glob += random.sample( string.acii_lowercase, options.lower_count )
        
    if options.special: 
        if options.special_count: 
            glob += random.sample( string.punctuation, options.special_count )
        
    if options.upper:
        if options.upper_count: 
            glob += random.sample( string.ascii_uppercase, options.upper_count )
        
    print glob
    
    return None

if __name__ == '__main__': 
    run_program()